package subject

import (
	"github.com/gin-gonic/gin"
	"smartLearn/models"
	"smartLearn/modules/response"
	"strconv"
)


func GetSubjectAll(c *gin.Context) {
	subjectModel := models.SystemSubject{};
	subjectList,err:=subjectModel.GetAll()
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	var list = map[string] []models.SystemSubject{
		"list":subjectList,
	}

	response.ShowData(c,list)
	return

}
func Add(c *gin.Context) {
	name:=c.Query("name")
	subjectModel := models.SystemSubject{};
	subjectModel.Name=name
	err:=subjectModel.Add()
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	response.ShowSuccess(c,"添加学科成功" )

}

func Update(c *gin.Context) {

	subjectModel := models.SystemSubject{};
	subjectModel.Name=c.Query("name")
	var err error
	subjectModel.Id,err =strconv.Atoi(c.Query("id"))
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}

	err=subjectModel.Update()
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	response.ShowSuccess(c,"更新学科成功" )

}


func Delete(c *gin.Context) {

	subjectModel := models.SystemSubject{};

	var err error
	subjectModel.Id,err =strconv.Atoi(c.Query("id"))
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}

	err=subjectModel.Delete()
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	response.ShowSuccess(c,"删除学科成功" )

}
